﻿using Microsoft.AspNetCore.Mvc;
using Qy.Core;
using Qy.Scenario;
using Qy.Web.Authorize;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Qy.Web.Areas.Api;

[Route("Api/[controller]/[action]")]
[Verify]
public class UserPostController : Base
{
    private readonly UserPostService _UserPostService;

    public UserPostController(UserPostService adminUserPostService)
    {
        _UserPostService = adminUserPostService;
    }

    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.UserPost_Index)]
    public async Task<IActionResult> IndexAsync()
    {
        List<UserPost> pagedList = await _UserPostService.GetAllAsync();
        return Success(new { data = pagedList });
    }

    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.UserPost_AddOrEditView)]
    public async Task<IActionResult> AddOrEditAsync(int id)
    {
        UserPost data = await _UserPostService.GetInfo(id);
        return Success(new { data });
    }

    [HttpPost, ActionName("Create")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.UserPost_AddOrEdit)]
    [Log(Name = "新增岗位")]
    public async Task<IActionResult> CreatePostAsync([FromBody] UserPost userPost)
    {
        UserPost insertPri = await _UserPostService.InsertAndDelCacheAsync(userPost);
        if (insertPri != null)
            return Success("新增岗位 ID:" + insertPri.ToString());
        else
            return Fail("新增岗位错误，请查看控制台日志！");
    }

    [HttpPut, ActionName("Edit")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.UserPost_AddOrEdit)]
    [Log(Name = "修改岗位")]
    public async Task<IActionResult> EditPostAsync([FromBody] EditUserPost userPost, int dyEdit = 0)
    {
        UserPost adminUserDept = await _UserPostService.GetInfo(userPost.Id);
        if (dyEdit == 1)
        {
            adminUserDept.Sort = userPost.Sort;
            if (0 < await _UserPostService.UpdateAsync(adminUserDept))
                return Success("修改岗位排序:" + adminUserDept.PostName + " Id： " + userPost.Id);
            else
                return Fail("修改岗位排序失败:" + adminUserDept.PostName + " Id： " + userPost.Id);
        }
        adminUserDept.PostName = userPost.PostName;
        adminUserDept.PostCode = userPost.PostCode;
        adminUserDept.Ico = userPost.Ico;
        adminUserDept.Sort = userPost.Sort;
        adminUserDept.Remark = userPost.Remark;
        if (0 < await _UserPostService.UpdateAndDelCacheAsync(adminUserDept))
            return Success("修改岗位:" + adminUserDept.PostName + " Id： " + userPost.Id);
        else
            return Fail("修改岗位:" + adminUserDept.PostName + " Id： " + userPost.Id);
    }

    [HttpDelete, ActionName("Delete")]
    [ActionPermissionFilter(Permission = PermissionKeys.UserPost_Delete)]
    [Log(Name = "删除岗位")]
    public async Task<IActionResult> DeleteAsync(int id)
    {
        if (await _UserPostService.DeleteAndDelCacheAsync(id) > 0)
            return Success("删除岗位,Id： " + id);
        else
            return Fail("删除失败,Id： " + id);
    }
}
